package com.fosun.spr.plugin.uuc.api.uucPrivilege.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fosun.spr.plugin.uuc.api.uucPrivilege.dao.UucPrivilegeDao;
import com.fosun.spr.plugin.uuc.api.uucPrivilege.service.UucPrivilegeService;
import com.fosun.spr.plugin.uuc.api.uucPrivilege.service.model.CreateOrUpdateSpModel;
import com.fosun.spr.plugin.uuc.api.uucPrivilege.service.model.CreateOrUpdateSrModel;
import com.fosun.spr.plugin.uuc.api.uucPrivilege.service.model.DeleteSpModel;
import com.fosun.spr.plugin.uuc.api.uucPrivilege.service.model.DeleteSrModel;
import com.fosun.spr.plugin.uuc.api.uucPrivilege.service.model.GetSpModel;
import com.fosun.spr.plugin.uuc.api.uucPrivilege.service.model.GetSrModel;
import com.fosun.spr.plugin.uuc.api.uucPrivilege.service.model.ListSpModel;
import com.fosun.spr.plugin.uuc.api.uucPrivilege.service.model.ListSrModel;
import com.fosun.spr.plugin.uuc.helper.mybatis.dao.SprUucPrivilegeMapper;
import com.fosun.spr.plugin.uuc.helper.mybatis.model.SprUucPrivilege;
import com.fosun.spr.plugin.uuc.helper.mybatis.model.SprUucPrivilege;
import com.fosun.spr.plugin.uuc.helper.mybatis.model.SprUucPrivilegeExample;
import com.fosun.spr.core.base.CoBaseService;
import com.fosun.spr.core.exception.ServiceException;
import com.fosun.spr.core.utils.ExtAsserts;


@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
@Service
public class UucPrivilegeServiceImpl extends CoBaseService implements
		UucPrivilegeService {
	@Autowired 
	private UucPrivilegeDao uucPrivilegeDao;
	@Autowired
	private SprUucPrivilegeMapper sprUucPrivilegeMapper;

	@Override
	public ListSrModel list(ListSpModel spModel) {
		ListSrModel srModel = new ListSrModel();
		srModel.setDatas(uucPrivilegeDao.list(spModel));
		srModel.setTotalCount(uucPrivilegeDao.listCount(spModel));
		return srModel;
	}

	@Override
	public GetSrModel get(GetSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		GetSrModel srModel = new GetSrModel();
		SprUucPrivilege db = sprUucPrivilegeMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db,"权限管理");
		cglibCopyProperties(db, srModel, true);	
		return srModel;
	}
	
	@Override
	public DeleteSrModel delete(DeleteSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		DeleteSrModel srModel = new DeleteSrModel();
		SprUucPrivilege db = sprUucPrivilegeMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db);
		db.setIsDeleted(IS_DELETED_Y);
		db.setRecUpdateTime(new Date());
		sprUucPrivilegeMapper.updateByPrimaryKey(db);
		return srModel;
	}
	
	@Override
	public CreateOrUpdateSrModel createOrUpdate(CreateOrUpdateSpModel spModel) {
		ExtAsserts.notNull(spModel.getPrivilegeCode(),"权限代码");
		ExtAsserts.notNull(spModel.getPrivilegeName(),"权限名称");
		
		CreateOrUpdateSrModel srModel = new CreateOrUpdateSrModel();
		//唯一性校验
		SprUucPrivilegeExample ePrivilegeCode = new SprUucPrivilegeExample();
		SprUucPrivilegeExample.Criteria cPrivilegeCode = ePrivilegeCode.createCriteria();
		cPrivilegeCode.andPrivilegeCodeEqualTo(spModel.getPrivilegeCode());
		cPrivilegeCode.andIsDeletedEqualTo(IS_DELETED_N);
		List<SprUucPrivilege> rowsPrivilegeCode = sprUucPrivilegeMapper.selectByExample(ePrivilegeCode);		
		
		if(StringUtils.isBlank(spModel.getId())){
			if(rowsPrivilegeCode.size()>0) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getPrivilegeCode()+" 已经存在");
			}		
			
			SprUucPrivilege db = new SprUucPrivilege();
			cglibCopyProperties(spModel, db, true);
			db.setIsDeleted(IS_DELETED_N);
			db.setRecCreateTime(new Date());
			db.setId(getUuid());
			sprUucPrivilegeMapper.insert(db);
			srModel.setId(db.getId());
		}else{
			if(rowsPrivilegeCode.size()>1) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getPrivilegeCode()+" 已经存在");
			}			
			if(rowsPrivilegeCode.size()==1 && !rowsPrivilegeCode.get(0).getPrivilegeCode().equals(spModel.getPrivilegeCode())) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getPrivilegeCode()+" 已经存在");
			}	
			
			SprUucPrivilege db = sprUucPrivilegeMapper.selectByPrimaryKey(spModel.getId());
			db.setPrivilegeCode(spModel.getPrivilegeCode());
			db.setPrivilegeName(spModel.getPrivilegeName());
			db.setMemo(spModel.getMemo());
			sprUucPrivilegeMapper.updateByPrimaryKey(db);
		}
		return srModel;
	}	
}
